#include <stdio.h>
#include <stdlib.h>

#define MAX_LENGTH 512

typedef struct IntArrayStack
{
    int items[MAX_LENGTH];
    int top;
} intarraystack;

typedef struct IntResizingArrayStack
{
    int *pitems;
    int count;
    int top;
} intresizingstack;

typedef struct IntNode
{
    int item;
    struct IntNode *pnext;
} intnode;

typedef struct IntListStack
{
    intnode *ptop;
    int count;
} intliststack;

typedef struct IntArrayQueue
{
    int items[MAX_LENGTH];
    int head;
    int rear;
    int count;
} intarrayqueue;

typedef struct IntListQueue
{
    intnode *phead;
    intnode *prear;
    int count;
} intlistqueue;

int intpush(int item,intarraystack *pstack)
{
    if(pstack->top == MAX_LENGTH - 1)
    {
        printf("Stack is Full!");
        return -1;
    }
    else
    {
        pstack->top++;
        pstack->items[pstack->top]=item;
        return 0;
    }
}

int intpop(intarraystack *pstack)
{
    if(pstack->top == -1)
    {
        printf("Stack is Empty!");
        return -1;
    }
    else
    {
        int topitem = pstack->items[pstack->top];
        pstack->top--;
        return topitem;
    }
}

int initintarraystack(intarraystack *pstack)
{
    for(int i = 0; i < MAX_LENGTH; i++)
    {
        pstack->items[i] = 0;
    }
    pstack->top = -1;
    return 0;
}

int initintliststack(intliststack *pstack)
{
    pstack->ptop = NULL;
    pstack->count = 0;
}

int intlistpush(int item, intliststack *pstack)
{
    intnode *pold = pstack->ptop;
    pstack->ptop=(intnode*)calloc(1,sizeof(intnode));
    if(pstack->ptop == NULL)
    {
        printf("\nAllocate Memory Error!\n");
        return -1;
    }
    else
    {
        pstack->ptop->item = item;
        pstack->ptop->pnext=pold;
        pstack->count++;
        return 0;
    }
}

int intlistpop(intliststack *pstack)
{
    if(pstack->count == 0 || pstack->ptop == NULL)
    {
        printf("\nStack is Empty!\n");
        return -1;
    }
    else
    {
        int item = pstack->ptop->item;
        pstack->ptop = pstack->ptop->pnext;
        pstack->count--;
        return item;
    }
}

int intarrayenqueue(int item, intarrayqueue *pqueue)
{
    if(pqueue->count == MAX_LENGTH)
    {
        printf("Queue is FULL!");
        return -1;
    }
    else
    {
        pqueue->items[pqueue->rear] = item;
        pqueue->rear++;
        pqueue->count++;
        return 0;
    }
}

int intarraydequeue(intarrayqueue *pqueue)
{
    if(pqueue->count == 0 || pqueue->head == pqueue->rear)
    {
        printf("Queue is Empty!");
        return -1;
    }
    else
    {
        int item = pqueue->items[pqueue->head];
        pqueue->count--;
        pqueue->head++;
        return item;
    }
}

int initintarrayqueue(intarrayqueue *pqueue)
{
    for(int i = 0; i < MAX_LENGTH; i++)
    {
        pqueue->items[i] = 0;
    }
    pqueue->head = 0;
    pqueue->rear = 0;
    pqueue->count = 0;
    return 0;
}

int initintlistqueue(intlistqueue *pqueue)
{
    pqueue->phead = NULL;
    pqueue->prear = NULL;
    pqueue->count = 0;
    return 0;
}

int intlistenqueue(int item, intlistqueue *pqueue)
{
    intnode *pnewnode = (intnode*)calloc(1,sizeof(intnode));
    if(pnewnode == NULL)
    {
        printf("\nAllocate Memory Error!\n");
        return -1;
    }
    else 
    {
        pnewnode->item = item;
        if(pqueue->prear == NULL && pqueue->phead == NULL)
        {
            pqueue->phead = pnewnode;
            pqueue->prear = pnewnode;
        }
        else
        {
            pqueue->prear->pnext=pnewnode;
            pqueue->prear=pnewnode;
        }
        pqueue->count++;
        return 0;
    }
}

int intlistdequeue(intlistqueue *pqueue)
{
    if(pqueue->count == 0 || pqueue->phead == NULL)
    {
        printf("\nQueue is empty!\n");
        return -1;
    }
    else
    {
        int item = pqueue->phead->item;
        intnode *pdequeue = pqueue->phead;
        if(pqueue->phead == pqueue->prear)
        {
            pqueue->phead = NULL;
            pqueue->prear = NULL;
        }
        else
        {
            pqueue->phead = pqueue->phead->pnext;
        }
        free(pdequeue);
        pqueue->count--;
        return item;
    }
}

int initintresizingstack(intresizingstack *pstack)
{
    pstack->pitems = (int*)calloc(MAX_LENGTH, sizeof(int));
    if(pstack->pitems == NULL)
    {
        printf("Allocate Memory Error!");
        return -1;
    }
    pstack->count = 0;
    pstack->top = 0;
    return 0;
}

int intresizestack(int n, intresizingstack *pstack)
{
    int *pold = pstack->pitems;
    pstack->pitems = (int*)calloc(pstack->count*2,sizeof(int));
    int i = 0;
    for(i = 0; i < pstack->count; i++);
    {
        pstack->pitems[i] = pold[i];
    }
    return 0;
}

